﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for PhaseGUI.xaml
    /// </summary>
    
    public partial class PhaseGUI : UserControl
    {
        private eKanbanWPF.Model.BasePhase phase = null;
        public event EventHandler Changed;

        void phase_Changed(eKanbanWPF.Model.BasePhase sender)
        {
            if (sender != null)
            {
                //if (sender.Width > 0)
                {
                    Width = sender.Width; 
                }
                //if (sender.Height > 0)
                {
                    Height = sender.Height; 
                }
                Canvas.SetLeft(this, sender.Left);
                Canvas.SetTop(this, sender.Top);
                if (sender is Model.Phase)
                {

                    eKanbanWPF.Model.Phase temp = sender as eKanbanWPF.Model.Phase;

                    txtPhaseName.Text = temp.Name + "\n" + temp.MaxFeature;
                    txtDefOfDone.Text = "Definition of Done:\n" + temp.DefinitionOfDone;

                    ToolTip = String.Format("{0}\n{1}", txtPhaseName.Text, temp.MaxFeature);
                }
                else
                {
                    ToolTip = sender.Name;
                    txtPhaseName.Text = sender.Name + "\n-----";
                }
            }
            else
            {
                ToolTip = "";
            }
            if (Changed != null)
            {
                Changed.Invoke(this, null);
            }
        }

        private void setPhase(eKanbanWPF.Model.BasePhase value)
        {
            if (phase != null)
            {
                phase.Changed -= phase_Changed;
            }
            phase = value;
            if (phase != null)
            {
                phase.Changed += phase_Changed;
                if (value is Model.Phase)
                {
                    txtDefOfDone.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    txtDefOfDone.Visibility = System.Windows.Visibility.Hidden;
                }
            }
            phase_Changed(value);
        }


        public eKanbanWPF.Model.BasePhase Phase
        {
            get { return phase; }
            set
            {
                setPhase(value);
            }
        }

        public PhaseGUI()
        {
            InitializeComponent();

            SetEvents();

            SetGuiProperties();
        }

        private void SetGuiProperties()
        {
            //grdContent.Background = this.Background;

        }

        private void SetEvents()
        {
            this.MouseDoubleClick += UserControl_MouseDoubleClick;
            this.PreviewMouseDoubleClick += UserControl_MouseDoubleClick;
            this.MouseUp += UserControl_MouseUp;
            this.PreviewMouseDown += UserControl_PreviewMouseDown;
            this.LostFocus += UserControl_LostFocus;

            this.Drop += new DragEventHandler(PhaseGUI_Drop);
            this.PreviewDrop += PhaseGUI_Drop;            
        }

        void PhaseGUI_Drop(object sender, DragEventArgs e)
        {
            DevExpress.XtraEditors.XtraMessageBox.Show("Dropped");                
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //phase.Width = Width;
            //phase.Height = Height;
            phase.Left = Canvas.GetLeft(this);
            phase.Top = Canvas.GetTop(this);

            var r = FrmPhase.Show(phase);
            if (r == true)
            {
                phase.Name = FrmPhase.frmPhase.PhaseName;
                if (phase is Model.Phase)
                {
                    ((Model.Phase)phase).MaxFeature = FrmPhase.frmPhase.MaxOfFeature;
                    ((Model.Phase)phase).DefinitionOfDone = FrmPhase.frmPhase.DefOfDone;
        
                    
                }
                phase.SetNextPhases(FrmPhase.frmPhase.NextPhases);
            }
        }

        private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, true);
            ((Control)sender).Focus();
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, false);
            if (phase != null)
            {
                phase.Width = this.Width;
                phase.Height = this.Height;
                phase.Left = Canvas.GetLeft(this);
                phase.Top = Canvas.GetTop(this);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }
            }

        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (phase != null)
            {
                phase.Width = this.Width;
                phase.Height = this.Height;
                phase.Left = Canvas.GetLeft(this);
                phase.Top = Canvas.GetTop(this);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }
            }
        }

    }
}
